package com.qf.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.qf.form.UserForm;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.support.SessionStatus;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;


@Controller
@RequestMapping("/user")
//@SessionAttributes(value = {"model"})
public class UserController {

    // 转发页面
    /* 转发页面

        @RequestMapping("/signin/{id}")
        public String signin(@PathVariable(value = "id") Integer uid, @Valid UserForm userForm, BindingResult bindingResult,
                             Model model){
            // 判断是否有问题
            if(bindingResult.hasErrors()){
                // 说明请求参数不满足注解的校验
                String message = bindingResult.getFieldError().getDefaultMessage();
                System.out.println(message);
            }
            // 1. 返回ModelAndView，相当于将数据存放到域对象中，并且转发页面
            //2. 返回String，返回结果相当于ViewName(页面路径)
            model.addAttribute("user",userForm);  //Model
            return "/WEB-INF/user.jsp";   // View
        }*/
    // 重定向
    /* 重定向
        http://ip:port/user/redirect1
            跳转到百度: redirect:http://www.baidu.com

        http://ip:port/user/redirect2
            跳转到TestController: redirect:/test

        http://ip:port/user/redirect3
            跳转到http://ip:port/user/test: redirect:test
         */
    /*

        //1. 重定向到ip:port
        @RequestMapping(value = "/redirect1",method = RequestMethod.GET)
        public String redirect1(){
            return "redirect:http://www.baidu.com";  // http://www.baidu.com
        }
        //2. 重定向到当前项目路径
        @GetMapping(value = "/redirect2")
        public String redirect2(){
            return "redirect:/test";        // http://localhost:8080/test
        }
        //3. 重定向到当前路径替换后一个路径
        @GetMapping(value = "/redirect3")
        public String redirect3(){
            return "redirect:test";   //http://localhost:8080/user/test
        }

        //4. 转发到路径
        @GetMapping(value = "/redirect4")
        public String redirect4(){
            return "forward:/test";
        }

    */
    // 传值
    /*

    @GetMapping("/reqmvc")
    public String reqmvc(Model model, ModelMap modelMap, HttpServletRequest req){
        model.addAttribute("model","model");
        modelMap.addAttribute("modelMap","modelMap");
        req.setAttribute("request","request");
        return "reqmvc";
    }

    @GetMapping("/sessionmvc")
    public String sessionMVC(HttpSession session, Model model, SessionStatus status){
        //==============删除Session域中的内容=======================
        session.invalidate();
        session.removeAttribute("session");
        status.setComplete();

        return "sessionmvc";
    }
*/


    // 接收/响应JSON数据  http://localhost:8080/user/json
    // jsonlib,jackson,gson,fastJSON
    @PostMapping("/json")
    @ResponseBody
    public UserForm json(@RequestBody UserForm user)  {
        ObjectMapper mapper = new ObjectMapper();
//        mapper.writeValueAsString(obj) -> JSON
//        UserForm userForm = mapper.readValue(json, UserForm.class);
//        Map map = mapper.readValue(json, Map.class);
        return user;
    }


}
